####################################################################################

mkdir -p ${Titan_path}/maf
mkdir -p ${Titan_path}/mut
mkdir -p ${Titan_path}/chat

## 纯度低于0.1的样本，没有CNV改变，纯度赋值为1
cat ${Titan_path}/Purity_titan.final.tsv | awk -F'\t' '{OFS="\t"}{if($2 < 0.1){$2=1}print}' \
> ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv

####################################################################################
################################### CHAT CCF
#### 1、得到每个样本没有header的maf
cat ${config_path}/tumor_normal.list | grep -v Normal | tr ',' ' ' | xargs -P 10 -i sh -c '
echo {}
sh ${scripts_path}/chat_ccf/TITAN_1_getMaf.sh {}
'

#### 2、得到CHAT输入格式，并注释突变信息
cat ${config_path}/tumor_normal.list | grep -v Normal | awk -F, '{print $1}' | sort -u | xargs -P 10 -i sh -c '
echo {}
${Rscript} ${scripts_path}/chat_ccf/TITAN_2_TranChatInput.R {} ${Titan_path}/maf ${config_path}/tumor_normal.list ${Titan_path}/mut
'

#### 3、计算CCF
## 使用最终人工检查的模型
cat ${config_path}/tumor_normal.list | grep -v Normal | awk -F, '{print $1}' | sort -u | \
xargs -P 10 -i sh -c '
${chat_rscript} ${scripts_path}/chat_ccf/TITAN_3_CHAT.R \
--mCRC_dir ${mCRC_DIR} \
--Sample {} \
--purity_file ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv \
--mut_file ${Titan_path}/mut/{}_Mut.tsv \
--seg_dir ${Titan_path}/FinalModel \
--out_path ${Titan_path}/chat
'

####################################################################################
################################### MutatationTime
## 建立Input
cat ${config_path}/tumor_normal.list | grep -v Normal | awk -F, '{print $1}' | sort -u | xargs -P 10 -i sh -c '
sh ${scripts_path}/mutationTime/MutationTime_GetInput.sh {}
'

## 检查是否所有样本都跑完
finishSample=`ls ${MutationTime_path}/result | grep mutTime.tsv  | awk -F'_CCF_mutTime' '{print $1}' | tr '\n' '|' | sed 's/|$//'`
## 无CNV改变样本
lowcnv_sample=`cat ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv | awk -F'\t' '{if($2==1)print $1}' | tr '\n' '|' | sed 's/|$//'`
## 存在CNV改变样本
changecnv_sample=`cat ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv | awk -F'\t' '{if($2<1)print $1}' | tr '\n' '|' | sed 's/|$//'`

## 计算突变时期
## 存在CNV改变样本,按照默认参数
cat ${config_path}/tumor_normal.list | grep -v Normal | grep -v -E -w ${finishSample} | awk -F, '{print $1}' | \
sort -u | awk NF | grep -E -w ${changecnv_sample} | xargs -P 40 -i sh -c '
${Rscript_mutationTime} ${scripts_path}/mutationTime/MutationTime_run.R \
--sample {} \
--ccf_file ${Titan_path}/chat/{}_CHAT.txt \
--vcf_file ${MutationTime_path}/input/{}.vcf \
--cnv_file ${MutationTime_path}/input/{}.seg \
--purity_file ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv \
--out_path ${MutationTime_path}/result/ \
--chr_len ${ref_path}/seq/GRCh37.fa.gz.fai \
--bedtools_path ${tools_path}/StandTools \
2> ${log_path}/{}_MutationTimeRun.log
'
## 无CNV改变样本,修改参数,主要为亚克隆
cat ${config_path}/tumor_normal.list | grep -v Normal | grep -v -E -w ${finishSample}  | awk -F, '{print $1}' | \
sort -u | awk NF | grep -E -w ${lowcnv_sample} | xargs -P 40 -i sh -c '
${Rscript_mutationTime} ${scripts_path}/mutationTime/MutationTime_run.lowPurity.R \
--sample {} \
--ccf_file ${Titan_path}/chat/{}_CHAT.txt \
--vcf_file ${MutationTime_path}/input/{}.vcf \
--cnv_file ${MutationTime_path}/input/{}.seg \
--purity_file ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv \
--out_path ${MutationTime_path}/result/ \
--chr_len ${ref_path}/seq/GRCh37.fa.gz.fai \
--bedtools_path ${tools_path}/StandTools \
2> ${log_path}/{}_MutationTimeRun.log
'

## 计算突变时期
## 存在CNV改变样本,按照默认参数
cat ${config_path}/tumor_normal.list | grep -v Normal | grep -v -E -w ${finishSample} | awk -F, '{print $1}' | \
sort -u | awk NF | grep -E -w ${changecnv_sample} | xargs -P 40 -i sh -c '
${Rscript_mutationTime} ${scripts_path}/mutationTime/MutationTime_run.R \
--sample {} \
--ccf_file ${Titan_path}/chat/{}_CHAT.txt \
--vcf_file ${MutationTime_path}/input/{}.vcf \
--cnv_file ${MutationTime_path}/input/{}.seg \
--purity_file ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv \
--out_path ${MutationTime_path}/result/ \
--chr_len ${ref_path}/seq/GRCh37.fa.gz.fai \
--bedtools_path ${tools_path}/StandTools \
2> ${log_path}/{}_MutationTimeRun.log
'

## 检查所有样本不同时期的突变数量
cat ${config_path}/tumor_normal.list | grep -v Normal | awk -F, '{print $1}' | sort -u | xargs -P 10 -i sh -c '
${Rscript} ${scripts_path}/mutationTime/MutationTime_GetTimeDistribution.R \
--sample {} \
--purity_file ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv \
--input_file ${MutationTime_path}/result/{}_CCF_mutTime.tsv  \
--out_path ${MutationTime_path}/distribution  \
2> ${log_path}/{}_MutationTime_GetTimeDistribution.log
'

## 每个样本，不同时期的克隆突变数目有多少
rm -rf ${MutationTime_path}/distribution/All_mutTime.tsv
cat ${MutationTime_path}/distribution/* | grep Purity | head -1 > ${MutationTime_path}/distribution/All_mutTime.tsv
for Tumor in `cat ${config_path}/tumor_normal.list | grep -v Normal | awk -F, '{print $1}' | sort -u`
do
cat ${MutationTime_path}/distribution/${Tumor}_mutTime_stat.tsv | grep -v Purity >> ${MutationTime_path}/distribution/All_mutTime.tsv
done

####################################################################################
## 合并每个突变的克隆时期
rm -rf ${MutationTime_path}/result/All_CCF_mutTime.tsv 
cat ${MutationTime_path}/result/*CCF_mutTime.tsv | grep -m 1 Chr | awk -F'\t' '{OFS="\t"}{print "Sample",$0}' \
> ${MutationTime_path}/result/All_CCF_mutTime.tsv

for Tumor in `cat ${config_path}/tumor_normal.list | grep -v Normal | awk -F, '{print $1}' | sort -u`
do
echo ${Tumor}
cat ${MutationTime_path}/result/${Tumor}_CCF_mutTime.tsv | grep -v Chr | awk -F'\t' '{OFS="\t"}{print Sample,$0}' Sample=${Tumor} \
>> ${MutationTime_path}/result/All_CCF_mutTime.tsv
done
